package io.gitee.zhangbinhub.acp.boot.component

import io.gitee.zhangbinhub.acp.boot.constant.AcpCoreConstant
import io.gitee.zhangbinhub.acp.boot.tools.IpTools
import jakarta.servlet.Filter
import jakarta.servlet.FilterChain
import jakarta.servlet.ServletRequest
import jakarta.servlet.ServletResponse
import jakarta.servlet.annotation.WebFilter
import jakarta.servlet.http.HttpServletRequest

@WebFilter(urlPatterns = ["/**"])
class ServletWebFilter : Filter {
    override fun doFilter(request: ServletRequest, response: ServletResponse, chain: FilterChain) {
        if (request is HttpServletRequest) {
            IpTools.getRemoteIP(request)?.also { remoteIp ->
                request.session.setAttribute(AcpCoreConstant.HTTP_CLIENT_IP_KEY, remoteIp)
            }
        }
        chain.doFilter(request, response)
    }
}